package com.aispeech.jarpc.op.wfst;

import com.aispeech.jarpc.TextBin;
import com.aispeech.jarpc.op.AbstractRequest;
import org.apache.commons.lang3.StringUtils;

public class WfstOfflineRequest extends AbstractRequest {
    private final static String REQ_TMPL = "{\"op\":\"wfst\",\"env\":\"%s\"}";
    private final static String ENV_TMPL_JSON = "{\\\"env\\\":\\\"%s\\\",\\\"buildinSkillRoot\\\":\\\"%s\\\",\\\"buildinSkillList\\\":[{\\\"skillId\\\":\\\"%s\\\",\\\"version\\\":\\\"%s\\\",\\\"skillName\\\":\\\"%s\\\"}]}";

    private String env;
    private String buildinSkillRoot;
    private String buildinSkillId;
    private String buildinSkillVersion;
    private String buildinSkillName;
    private byte[] pcmData;

    public WfstOfflineRequest(String env, byte[] pcmData) {
        this(env, null, null, "latest", "utterances", pcmData);
    }

    public WfstOfflineRequest(String env, String buildinSkillRoot, String buildinSkillId, byte[] pcmData) {
        this(env, buildinSkillRoot, buildinSkillId, "latest", "utterances", pcmData);
    }

    public WfstOfflineRequest(String env, String buildinSkillRoot, String buildinSkillId, String buildinSkillVersion, String buildinSkillName, byte[] pcmData) {
        this.env = env;
        this.buildinSkillRoot = buildinSkillRoot;
        this.buildinSkillId = StringUtils.isAnyEmpty(buildinSkillId) ? null : buildinSkillId;
        this.buildinSkillVersion = buildinSkillVersion;
        this.buildinSkillName = buildinSkillName;
        this.pcmData = pcmData;
    }


    @Override
    public String OpType() {
        return "wfst";
    }

    @Override
    public String toJson() {
        String jenv = buildinSkillId == null ? env : String.format(ENV_TMPL_JSON, env, buildinSkillRoot, buildinSkillId, buildinSkillVersion, buildinSkillName);
        return String.format(REQ_TMPL, jenv);
    }

    public TextBin toJsonBin() {
        String json = toJson();

        return new TextBin(json, pcmData);
    }
}
